package com.xiaobawang.homework.demo.connerlt;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@EnableScheduling
public class AlarmTask {
    /*
     * 定义执行线程启动的开关
     * */
    @Scheduled(cron = "0/5 * * * * *")
    public void run() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println(Thread.currentThread().getName() + "---->使用时间：" + (System.currentTimeMillis() / 1000));
    }

    /*
     * 对上一次执行时间之后再执行的时间
     * */
    @Scheduled(fixedDelay = 5000)
    public void run1() throws InterruptedException {
        Thread.sleep(7000);
        System.out.println(Thread.currentThread().getName() + "---->使用时间：" + (System.currentTimeMillis() / 1000));
    }

    private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

    //每次执行完2s后再执行
    @Scheduled(fixedDelayString = "2000")
    public void run2() throws InterruptedException {
        System.out.println("fixedDelayString的时间为:" + format.format(new Date()));
    }

    //延时1s,每隔2s
    @Scheduled(fixedDelayString = "2000", initialDelay = 1000)
    public void run3() throws InterruptedException {
        System.out.println("每隔2s并且延时1s，再执行:" + format.format(new Date()));
    }


}
